package com.ruoyi.video.task;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.video.domain.VideoServer;
import com.ruoyi.video.server.AbstractServer;
import com.ruoyi.video.service.IVideoServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class ServerApplicationRunner implements ApplicationRunner {
    private static Map<String, AbstractServer> handlerMap = new ConcurrentHashMap<>();
    private static IVideoServerService videoServerService;

    @Autowired
    public void setVideoServerService(IVideoServerService videoServerService){
        this.videoServerService = videoServerService;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        for(AbstractServer handler : SpringUtils.getBeanList(AbstractServer.class)){
            handlerMap.put(handler.getCode(), handler);
        }
        //独立线程
        new Thread(()->{
            startAll();
        }).start();
    }

    /**
     * 启动所有启用的服务
     */
    private void startAll(){

        VideoServer vsc = new VideoServer();
        vsc.setStatus("1");
        List<VideoServer> list = videoServerService.selectVideoServerList(vsc);

        for(VideoServer videoServer:list){
            try {
                if (start(videoServer)) {
                    log.info(videoServer.getName() + " 插件启动成功");
                    videoServer.setRunning(VideoServer.STATE_RUNNING);
                } else {
                    log.error(videoServer.getName() + " 插件启动失败");
                    videoServer.setRunning(VideoServer.STATE_STOP);
                }
                videoServerService.updateVideoServer(videoServer);
            }catch (Exception e){
                e.printStackTrace();
                //zaSysErrorService.log(ZaSysError.TYPE_SERVER, platform.getName()+"启动失败", e.getMessage(), platform.getConfig());
            }
        }
    }

    /**
     * 获取服务处理接口
     * @param serverCode
     * @return
     */
    public static AbstractServer getHandler(String serverCode){
        return handlerMap.get(serverCode);
    }

    /**
     * videoServer
     * @param videoServer
     * @return
     */
    public static boolean start(VideoServer videoServer){
        if(!handlerMap.containsKey(videoServer.getCode())){
            log.error( "不支持平台{} - {}的接入", videoServer.getCode(), videoServer.getName());
            return false;
        }
        AbstractServer handler = handlerMap.get(videoServer.getCode());

        boolean ret = handler.start(videoServer);
        if(ret && (videoServer.getRunning() == null || videoServer.getRunning().equalsIgnoreCase(VideoServer.STATE_STOP))){
            videoServer.setRunning(VideoServer.STATE_RUNNING);
            videoServerService.updateVideoServer(videoServer);
        }
        return ret;
    }

    /**
     * 停止
     * @param videoServer
     * @return
     */
    public static boolean stop(VideoServer videoServer){
        if(!handlerMap.containsKey(videoServer.getCode())){
            log.error( "不支持平台{} - {}的接入", videoServer.getCode(), videoServer.getName());
            return false;
        }
        AbstractServer handler = handlerMap.get(videoServer.getCode());

        boolean ret = handler.stop(videoServer);
        if(ret && videoServer.getRunning().equalsIgnoreCase(VideoServer.STATE_RUNNING)){
            videoServer.setRunning(VideoServer.STATE_STOP);
            videoServerService.updateVideoServer(videoServer);
        }
        return ret;
    }
}
